<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

//引入hash

/**
 * 爵位配置
 * Class NobilityConfigRepository
 *
 * @package App\Repositories
 */
class NobilityConfigRepository
{
    /**
     * Notes:获取爵位列表
     * User: wangyueli
     *
     * @return \App\Models\MainDB\NobilityConfig|\Laravel\Lumen\Application|mixed
     */
    public function NobilityList($params)
    {
        $params['perPage'] = $params['perPage'] ?? 15;
        $list = app('model_nobility_config');
        $list = $list->where('deleted', 0)->orderBy('level')->orderBy('id')->paginate($params['perPage'])->toArray();

        return $list;
    }

    /**
     * 添加爵位
     *
     * @param $params
     * @return bool
     * @throws BasicException
     */
    public function nobilityAdd($params = [])
    {
        //判断级别不能重复
        $info = app('model_nobility_config')->where('deleted', 0)->where('level', $params['level'])->first();
        if ($info) {
            throw new BasicException(0, '等级不能重复');
        }

        //判断爵位权限是否存在
        if (isset($params['auth_ids'])) {
            $auth_ids = explode(',', $params['auth_ids']);
            $auth_ids_value = app('model_nobility_auth')->where('status', 1)->pluck('id')->toArray();
            $ids = array_intersect($auth_ids, $auth_ids_value);
            $params['auth_ids'] = implode(',', $ids);
        }

        $params['add_time'] = date('Y-m-d H:i:s');
        $params['update_time'] = date('Y-m-d H:i:s');

        $result = app('model_nobility_config')->create($params);
        if ($result) {
            return $result;
        }
        throw new BasicException(93013);
    }


    /**
     * 编辑爵位
     *
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function nobilityEdit($params)
    {
        //判断数据是否存在
        $nobilityDetail = app('model_nobility_config')->where('id', $params['id'])->where('deleted', 0)->first();
        if (empty($nobilityDetail)) {
            throw new BasicException(50002);
        }

        //判断级别不能重复
        $info = app('model_nobility_config')->where('deleted', 0)->where('level', $params['level'])->first();
        if ($info && $nobilityDetail->id != $info['id']) {
            throw new BasicException(0, '等级不能重复');
        }

        //判断爵位权限是否存在
        if (isset($params['auth_ids'])) {
            $auth_ids = explode(',', $params['auth_ids']);
            $auth_ids_value = app('model_nobility_auth')->where('status', 1)->pluck('id')->toArray();
            $ids = array_intersect($auth_ids, $auth_ids_value);
            $params['auth_ids'] = implode(',', $ids);
        }

        $params['update_time'] = date('Y-m-d H:i:s');
        $rankSave = app('model_nobility_config')->where('id', $params['id'])->update($params);
        if ($rankSave !== false) {
            return true;
        }
        throw new BasicException(93014);
    }


    /**
     * 删除爵位
     *
     * @param $params
     * @return bool
     * @throws BasicException
     */
    public function nobilityDel($params)
    {
        DB::transaction(function () use ($params) {
            foreach ($params as $v) {
                $level = 100000 + $v;
                app('model_nobility_config')->whereIn('id', $params)->update(['deleted' => 1, 'level' => $level]);
            }
        });
    }

    /**
     * 爵位详情
     *
     * @param $id
     * @return bool
     * @throws BasicException
     */
    public function nobilityDetail($id)
    {
        $nobilityDetail = app('model_nobility_config')->where('id', $id)->where('deleted', 0)->first();
        if ($nobilityDetail) {
            return $nobilityDetail;
        }
        throw new BasicException(50002);
    }


}


?>

